<?php
class Commsysnotify extends Base_Controller
{
    public function __construct()
    {
        $this->_page_size = 20;
        $this->_form_type = 'comm_sysnotify';
        $this->_list_where = array();
        $this->_action_list = array('EDIT','DEL',
            array('controller'=>'admin/commsysnotify/','function'=>'sendmsg','label'=>'发送','class'=>'sendnotify'));
        $this->_controller = 'admin/commsysnotify/';//admin/user/
        parent::__construct();
    }

    public function showList($page = 1)
    {

        $limit = $this->_page_size;
        $offset = ($page-1) * $limit;
        $pdata = $this->getPagination($this->_controller.'showList/');

        $res = $this->getList($offset,$limit);

        //filter list data
        foreach ($res['list'] as &$row){
            $row['external']=$row['external']==0?'内部链接':'外部链接';
            $row['created']=date('Y-m-d H:i:s',$row['created']);
            $row['issend']=$row['issend']==0?'未发送':'已发送';
        }//处理表数据

        //有创建按钮
        $createbtn = $this->load->view('admin/form/createbtn',array('controller'=>site_url($this->_controller)),true);

        $data = array('headers'=>$res['headers'],'list'=>$res['list'],'pagination'=>$pdata['pagination'],'createbtn'=>$createbtn);
        $content = $this->load->view('admin/form/list',$data,true);

        echo $this->getAdminView($content,'sysnotify.js');
    }

    public function update($id = 0)
    {
        $post = $this->input->post();
        if(empty($post)){
            $form = $this->updateBuildForm($id);
            $content = $this->load->view('admin/form/edit',array('controller'=>site_url($this->_controller),'form'=>$form),true);
            echo $this->getAdminView($content);
        }else{
            /******待编辑******/
            //filter post data
            $post['title'] = trim($post['title']);
            $post['msg'] = trim($post['msg']);
            $post['link'] = trim($post['link']);
            $post['external'] = (int)$post['external'];

            $this->_update_data = $post;
            parent::update($id);
        }
    }
    public function afterupdate($pkey,$pkeyval){}

    public function create()
    {
        $post = $this->input->post();
        if(empty($post)){
            $form = $this->createBuildForm();
            $content = $this->load->view('admin/form/edit',array('controller'=>site_url($this->_controller),'form'=>$form),true);
            echo $this->getAdminView($content);
        }else{
            /******待编辑******/
            //filter post data
            $post['title'] = trim($post['title']);
            $post['msg'] = trim($post['msg']);
            $post['link'] = trim($post['link']);
            $post['external'] = (int)$post['external'];
            $post['created'] = time();

            $this->_create_data = $post;
            parent::create();
        }
    }

    public function aftercreate($pkeyval){}

    public function sendmsg($pkeyval)
    {
        $res=$this->adminform_model->getRowData('comm_sysnotify','*',array('id'=>$pkeyval));
        $notifydeliver = array();
        $notifydeliver['type']='sys';
        $notifydeliver['data']['title']=$res['title'];
        $notifydeliver['data']['msg']=$res['msg'];
        $notifydeliver['data']['link']=$res['link'];
        $notifydeliver['data']['external']=$res['external'];
        $notifydeliver['data']['created']=$res['created'];

        $notifydeliver=igbinary_serialize($notifydeliver);
        $this->load->model('redis_model');
        $redis = $this->redis_model->getRedisInstance();
        $succ = $redis->rPush('notifydeliver',$notifydeliver);
        if ($succ !== false) $this->adminform_model->update('comm_sysnotify',array('issend'=>1),'id',$pkeyval);
        redirect(site_url($this->_controller.'showList/1'));
    }

    public function delete($id = 0)
    {
        parent::delete($id);
    }

    public function afterdelete($pkey,$pkeyval){}

}